package com.wintop.youyuan.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.wintop.youyuan.model.BaseData;
import com.wintop.youyuan.model.DetailData;
import com.wintop.youyuan.model.PersonalsCondition;
import com.wintop.youyuan.model.Photo;
import com.wintop.youyuan.model.User;
import com.wintop.youyuan.services.MailBoxServices;
import com.wintop.youyuan.services.UserInformationServices;
import com.wintop.youyuan.services.UserServices;
import com.wintop.youyuan.util.ActionUtil;
import com.wintop.youyuan.util.CalculateUtil;

@Scope("prototype")
@Controller
public class SpaceAction extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware, ServletResponseAware {
	private static final long serialVersionUID = 1L;
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;
	private HttpServletResponse response;
	private User user;
	private UserServices userServices;
	private UserInformationServices userInformationServices;
	private MailBoxServices mailBoxServices;
	private BaseData baseData;
	private DetailData detailData;
	private PersonalsCondition personalsCondition;
	private HashMap<String, Object> otherData;// 临时数据储存
	private int type;// 跳转类型

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	/*
	 * 空间首页
	 */
	public String redirect() {

		/*
		 * User userInfo = new User(26, "xu@qq.com",
		 * "e10adc3949ba59abbe56e057f20f883e", "许峰", 0, 1, null);
		 * session.put("userInfo", userInfo);
		 */

		user = (User) session.get("userInfo");
		if (user == null) {// 判断用户是否登录
			return "error";
		}
		// baseData = userInformationServices.getBaseData(user.getUserid());
		otherData = new HashMap<String, Object>();
		otherData.put(
				"icon",
				ActionUtil.getIcon(user.getUserid(),
						userInformationServices.getIcon(user.getUserid())));
		otherData.put("messageNum",
				mailBoxServices.getMessageNum(user.getUserid()));
		String redirectStr = null;
		switch (type) {
		case 0:
			redirectStr = "redirectSpace";
			break;
		case 1:
			redirectStr = "redirectServe";
			break;
		case 2:
			redirectStr = "redirectMailbox";
			break;
		}
		return redirectStr;
	}

	public String redirectToday() {// 今日速配
		return "redirectToday";
	}

	public String redirectBaseData() {// 基本资料
		user = (User) session.get("userInfo");
		otherData = new HashMap<String, Object>();
		String sex = ActionUtil.getSex(user.getSex());
		otherData.put("sex", sex);
		baseData = userInformationServices.getBaseData(user.getUserid());
		int age = CalculateUtil.getAge(baseData.getBirthday());
		otherData.put("age", age);
		String constellation = CalculateUtil.getConstellation(baseData
				.getBirthday());
		otherData.put("constellation", constellation);
		return "redirectBaseData";
	}

	public String redirectDetails() {// 详细资料
		user = (User) session.get("userInfo");
		detailData = userInformationServices.getDetailData(user.getUserid());
		return "redirectDetails";
	}

	public String redirectSoliloquy() {// 内心独白
		user = (User) session.get("userInfo");
		detailData = userInformationServices.getDetailData(user.getUserid());
		return "redirectSoliloquy";
	}

	public String redirectUploadingIcon() {// 上传头像
		user = (User) session.get("userInfo");
		baseData = userInformationServices.getBaseData(user.getUserid());
		baseData.setIcon(ActionUtil.getIcon(user.getUserid(),
				baseData.getIcon()));
		return "redirectUploadingIcon";
	}

	public String redirectMyPhoto() {// 我的相册
		user = (User) session.get("userInfo");
		otherData = new HashMap<String, Object>();
		List<Photo> photos = userInformationServices
				.getPhotos(user.getUserid());
		for (Photo p : photos) {
			p.setSrc(ActionUtil.getIcon(user.getUserid(), p.getSrc()));
		}
		otherData.put("photos", photos);
		return "redirectMyPhoto";
	}

	public String redirectPersonalCondition() {// 征友条件
		user = (User) session.get("userInfo");
		personalsCondition = userInformationServices.getPersonalsCondition(user
				.getUserid());
		return "redirectPersonalCondition";
	}

	public String redirectChangePassword() {// 修改密码
		return "redirectChangePassword";
	}

	public String redirectLogout() {// 退出
		session.remove("userInfo");
		return "error";
	}

	public String redirectPersonalSpace() {// 个人空间
		otherData = new HashMap<String, Object>();
		if (user.getId() > 0) {
			user = userServices.getUser(user.getId());
		} else {
			user = userServices.getUser(user.getUserid());
		}
		String sex = ActionUtil.getSex(user.getSex());
		otherData.put("sex", sex);
		baseData = userInformationServices.getBaseData(user.getUserid());
		detailData = userInformationServices.getDetailData(user.getUserid());
		personalsCondition = userInformationServices.getPersonalsCondition(user
				.getUserid());

		otherData.put("icon",
				ActionUtil.getIcon(user.getUserid(), baseData.getIcon()));
		otherData.put("apartment",
				ActionUtil.getApartment(baseData.getApartment()));
		otherData.put("birthplace",
				ActionUtil.getApartment(baseData.getBirthplace()));
		otherData.put("age", CalculateUtil.getAge(baseData.getBirthday()));
		otherData.put("height", ActionUtil.getHeight(baseData.getHeight()));
		otherData.put("weight", ActionUtil.getWeight(baseData.getWeight()));
		otherData.put("monthlyIncome",
				ActionUtil.getMonthlyIncome(baseData.getMonthlyIncome()));
		otherData.put("house", ActionUtil.getHouse(baseData.getHouse()));
		otherData.put("hobby", detailData.getHobby());
		otherData.put("personality", detailData.getPersonality());

		List<Photo> photoList = userInformationServices.getPhotos(user
				.getUserid());
		for (Photo p : photoList) {
			p.setSrc(ActionUtil.getIcon(user.getUserid(), p.getSrc()));
		}
		otherData.put("photoList", photoList);

		otherData.put("education",
				ActionUtil.getEducation(baseData.getEducation()));
		otherData.put("children",
				ActionUtil.getChildren(detailData.getIschildren()));
		otherData.put("loveType",
				ActionUtil.getLoveType(detailData.getLoveType()));
		otherData.put("premaritalSex",
				ActionUtil.getPremaritalSex(detailData.getPremaritalSex()));
		otherData.put("liveWithParents",
				ActionUtil.getLiveWithParents(detailData.getLiveWithParents()));
		otherData.put("marryCondition",
				ActionUtil.getMarryCondition(baseData.getMarryCondition()));
		otherData.put("profession",
				ActionUtil.getProfession(baseData.getProfession()));
		otherData.put("distanceLove",
				ActionUtil.getDistanceLove(detailData.getDistanceLove()));
		otherData.put("charm", ActionUtil.getCharm(detailData.getCharm()));
		otherData.put("soliloquy", detailData.getSoliloquy());
		otherData.put("personalEducation",
				ActionUtil.getEducation(personalsCondition.getEducation()));
		otherData.put("personalIcome",
				ActionUtil.getMonthlyIncome(personalsCondition.getIncome()));
		otherData.put("personalAddress",
				ActionUtil.getApartment(personalsCondition.getAddress()));
		boolean isAttention = mailBoxServices.getAttention(
				((User) session.get("userInfo")).getUserid(), user.getUserid());
		System.out.println("关注" + isAttention);
		otherData.put("isAttention", isAttention);
		return "redirectPersonalSpace";
	}

	public HashMap<String, Object> getOtherData() {
		return otherData;
	}

	public void setOtherData(HashMap<String, Object> otherData) {
		this.otherData = otherData;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Resource(name = "userServices")
	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	@Resource(name = "userInformationServices")
	public void setUserInformationServices(
			UserInformationServices userInformationServices) {
		this.userInformationServices = userInformationServices;
	}

	@Resource(name = "mailBoxServices")
	public void setMailBoxServices(MailBoxServices mailBoxServices) {
		this.mailBoxServices = mailBoxServices;
	}

	public BaseData getBaseData() {
		return baseData;
	}

	public void setBaseData(BaseData baseData) {
		this.baseData = baseData;
	}

	public DetailData getDetailData() {
		return detailData;
	}

	public void setDetailData(DetailData detailData) {
		this.detailData = detailData;
	}

	public PersonalsCondition getPersonalsCondition() {
		return personalsCondition;
	}

	public void setPersonalsCondition(PersonalsCondition personalsCondition) {
		this.personalsCondition = personalsCondition;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getApplication() {
		return application;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

}
